package dynamic;

import java.util.Scanner;

public class 最长公共子序列 {
    public static void solvedp(char a[],char b[],int dp[][]){

        for (int i=1;i< a.length;i++){
            for(int j=1;j< b.length;j++){
                if(a[i]==b[j]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else{
                    int m= dp[i-1][j]>=dp[i][j-1]?dp[i-1][j]:dp[i][j-1];
                    dp[i][j] = m;
                } } }
    }

    public static void main(String[] args) {
        int m=0,n=0;
        int dp[][] = new int[1001][10001];
        String s[] = new String[2];
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();

        for(int i=0;i<s.length;i++){
            s[i] = sc.next();
        }
        char a [] = new char[m+1];
        char b [] = new char[n+1];
        for(int i=0;i<m;i++){
            a[i+1] = s[0].charAt(i);
        }
        for(int i=0;i<n;i++){
            b[i+1] = s[1].charAt(i);
        }

        solvedp(a,b,dp);

        System.out.println(dp[m][n]);
    }
}
